package com.sskj.wallet.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Switch;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.SelfCoinBean;
import com.sskj.lib.model.room.SelfCoinBeanViewModel;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.CoinListBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.AddCoinActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.WALLET_ADD_COIN)//添加币种
public class AddCoinActivity extends BaseActivity<AddCoinActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @Inject
    SelfCoinBeanViewModel selfCoinBeanViewModel;
    private boolean isSelect;
    private List<SelfCoinBean> selfCoinBeans;
    private SlimAdapter slimAdapter;
    private List<CoinListBean> coinListBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_add_coin;
    }

    @Override
    public AddCoinActivityPresenter getPresenter() {
        return new AddCoinActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.wallet_addCoinActivity1_j));
        DaggerUserDataComponent.create().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.wallet_bdc))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.wallet_recy_item_add_coin, new SlimInjector<CoinListBean>() {
            @Override
            public void onInject(CoinListBean data, IViewInjector injector, List payloads) {

                injector
                        .imageCircle(R.id.ivCoin, CoinEnum.getByCode(data.getPname()).getRes())
                        .text(R.id.tvName, data.getPname())
                        .text(R.id.tvDesc, data.getName())
                        .with(R.id.switchBtn, view -> {
                            Switch switchBtn = (Switch) view;
                            switchBtn.setChecked(data.isSelect());
                        })
                ;
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        selfCoinBeanViewModel.getUsers().observe(this, selfCoinBeans -> {
            if (selfCoinBeans != null) {
                AddCoinActivity.this.selfCoinBeans = selfCoinBeans;
                if (coinListBeans == null) {
                    mPresenter.getData();
                } else {
                    updateUI(coinListBeans);
                }
            }
        });
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, i, view) -> {
                    CoinListBean dataItem = (CoinListBean) slimAdapter.getDataItem(i);
                    if (!dataItem.isSelect()) {
                        selfCoinBeanViewModel.insert(new SelfCoinBean(dataItem.getPname()));
                    } else {
                        SelfCoinBean selfCoinBean = new SelfCoinBean(dataItem.getPname());
                        selfCoinBeanViewModel.delete(selfCoinBean);
                    }
                });

    }

    public void updateUI(List<CoinListBean> data) {
        coinListBeans = data;
        Flowable.fromIterable(data)
                .map(coinListBean -> {
                    isSelect = false;
                    Flowable.fromIterable(selfCoinBeans)
                            .filter(selfCoinBean -> selfCoinBean.getCode().equals(coinListBean.getPname()))
                            .subscribe(selfCoinBean -> {
                                isSelect = true;
                            });
                    coinListBean.setSelect(isSelect);
                    return coinListBean;
                })
                .toList()
                .subscribe((coinListBeans, throwable) -> {
                    slimAdapter.updateData(coinListBeans);
                });
    }
}
